package com.yh.util;

import com.yh.javabean.User;
import mapper.UserDao;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

public class UserDaoTest {
    public static void main7(String[] args) throws IOException {
        SqlSessionFactory build = new SqlSessionFactoryBuilder()
                .build(Resources.getResourceAsStream("mybatis-config.xml"));
        SqlSession sqlSession = build.openSession(true);
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        HashMap<String,Object>   map=new HashMap<>();
        map.put("id",1);
        map.put("name","鲁智深");
        User user = mapper.selectByMap(map);

        System.out.println(user);
    }

    public static void main6(String[] args) throws IOException {
        SqlSessionFactory build = new SqlSessionFactoryBuilder()
                .build(Resources.getResourceAsStream("mybatis-config.xml"));
        SqlSession sqlSession = build.openSession(true);
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        ArrayList<Integer> list=new ArrayList<>();
        list.add(1);list.add(2);
        ArrayList<User> users = mapper.selectBylist(list);

        for (User user:users ) {
            System.out.println(user);

        }
    }
    public static void main5(String[] args) throws IOException {
        SqlSessionFactory build = new SqlSessionFactoryBuilder()
                .build(Resources.getResourceAsStream("mybatis-config.xml"));
        SqlSession sqlSession = build.openSession(true);
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        int[] arr={1,2};
        ArrayList<User> users = mapper.selectByArray(arr);
        for (User user:users ) {
            System.out.println(user);

        }

    }
    public static void main4(String[] args) throws IOException {
        SqlSessionFactory build = new SqlSessionFactoryBuilder()
                .build(Resources.getResourceAsStream("mybatis-config.xml"));
        SqlSession sqlSession = build.openSession(true);
        UserDao mapper = sqlSession.getMapper(UserDao.class);
      User u= new User(1, "鲁智深", 200, new Date());
        User user = mapper.selectByUser(u);
        System.out.println(user);


    }
    public static void main3(String[] args) throws IOException {
        SqlSessionFactory build = new SqlSessionFactoryBuilder()
                .build(Resources.getResourceAsStream("mybatis-config.xml"));
        SqlSession sqlSession = build.openSession(true);
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        User user = mapper.selectByIdandName(1,"鲁智深");
        System.out.println(user);

    }
    public static void main2(String[] args) throws IOException {
        SqlSessionFactory build = new SqlSessionFactoryBuilder()
                .build(Resources.getResourceAsStream("mybatis-config.xml"));
        SqlSession sqlSession = build.openSession(true);
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        User user = mapper.selectById(1);
        System.out.println(user);

    }
    public static void main1(String[] args) throws IOException {
        SqlSessionFactory build = new SqlSessionFactoryBuilder()
                .build(Resources.getResourceAsStream("mybatis-config.xml"));
        SqlSession sqlSession = build.openSession(true);
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        ArrayList<User> users = mapper.selectAllUser();
        for (User user:users) {
            System.out.println(user);
        }
        sqlSession.close();

    }
}
